﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("SQL", "GetDataService", "1.0")]
    public class GetSQLDataServiceAction : BaseAction<GetSQLDataServiceParams, GetSQLDataServiceResult>
    {
        protected override ActionResult<GetSQLDataServiceResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetSQLDataServiceParams opParams)
        {
            SER.DtSerService dtSerService = new SER.DtSerService();
            ServiceResult<VIW.VDtSer> serResult = dtSerService.GetDtSer(opParams.ServiceId, CPPEI.ETL.Domain.DEF.DataServiceTypeId.SQL);
            if (serResult.IsSuccess)
            {
                GetSQLDataServiceResult result = new GetSQLDataServiceResult();
                result.Name = serResult.Data.Name;
                string[] addressArr = serResult.Data.Address.Split(new char[] { ',' });
                result.Server = addressArr[0];
                result.Database = addressArr[1];
                result.NeedAuth = serResult.Data.NeedAuth;
                result.AuthUser = serResult.Data.AuthUser;
                result.Description = serResult.Data.Description;
                result.IsEnabled = serResult.Data.IsEnabled;
                return new ActionResult<GetSQLDataServiceResult>(result);
            }
            else
            {
                return new ActionResult<GetSQLDataServiceResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetSQLDataServiceParams
    {
        [Parameter]
        public Guid ServiceId { get; set; }
    }

    public class GetSQLDataServiceResult
    {
        public string Name { get; set; }

        public bool NeedAuth { get; set; }

        public string AuthUser { get; set; }

        public string Server { get; set; }

        public string Database { get; set; }

        public string Description { get; set; }

        public bool IsEnabled { get; set; }
    }
}
